home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / qltk / x.pyc (.txt) < prev   
Python Compiled Bytecode  |  2014-12-31  |  14KB  |  361 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from gi.repository import Gtk, GObject, GLib, Gio
  5. from quodlibet import util
  6. from quodlibet import config
  7. from quodlibet.qltk import add_css, is_accel
  8. from quodlibet.util import connect_obj
  9.  
  10. class ScrolledWindow(Gtk.ScrolledWindow):
  11.     """Draws a border around all edges that don't touch the parent window"""
  12.     
  13.     def do_size_allocate(self, alloc):
  14.         if self.get_shadow_type() == Gtk.ShadowType.NONE:
  15.             return Gtk.ScrolledWindow.do_size_allocate(self, alloc)
  16.         toplevel = None.get_toplevel()
  17.         top_window = toplevel.get_window()
  18.         window = self.get_window()
  19.         if not window:
  20.             GLib.idle_add(self.queue_resize)
  21.             return Gtk.ScrolledWindow.do_size_allocate(self, alloc)
  22.         (dummy, x1, y1) = None.get_origin()
  23.         (dummy, x2, y2) = window.get_origin()
  24.         dx = max(x2 - x1, 0)
  25.         dy = max(y2 - y1, 0)
  26.         ctx = self.get_style_context()
  27.         border = ctx.get_border(self.get_state_flags())
  28.         border.left = border.top = border.right = border.bottom = 1
  29.         
  30.         try:
  31.             top_bar = toplevel.top_bar
  32.             if not isinstance(top_bar, Gtk.Widget):
  33.                 raise TypeError
  34.         except (AttributeError, TypeError):
  35.             border.top = 0
  36.  
  37.         top_alloc = top_bar.get_allocation()
  38.         top_ctx = top_bar.get_style_context()
  39.         b = top_ctx.get_border(top_bar.get_state_flags())
  40.         if b.bottom:
  41.             dy -= top_alloc.y + top_alloc.height
  42.         bottom = left = right = top = False
  43.         value = GObject.Value()
  44.         value.init(GObject.TYPE_BOOLEAN)
  45.         value.set_boolean(True)
  46.         ctx.get_style_property('scrollbars-within-bevel', value)
  47.         scroll_within = value.get_boolean()
  48.         value.unset()
  49.         if not scroll_within:
  50.             h = self.get_hscrollbar()
  51.             v = self.get_vscrollbar()
  52.             hscroll = vscroll = False
  53.             if h.get_visible():
  54.                 req = h.size_request()
  55.                 hscroll = bool(req.width + req.height)
  56.             if v.get_visible():
  57.                 req = v.size_request()
  58.                 vscroll = bool(req.width + req.height)
  59.             placement = self.get_placement()
  60.             if placement == Gtk.CornerType.TOP_LEFT:
  61.                 bottom = hscroll
  62.                 right = vscroll
  63.             elif placement == Gtk.CornerType.BOTTOM_LEFT:
  64.                 right = vscroll
  65.                 top = hscroll
  66.             elif placement == Gtk.CornerType.TOP_RIGHT:
  67.                 bottom = hscroll
  68.                 left = vscroll
  69.             elif placement == Gtk.CornerType.BOTTOM_RIGHT:
  70.                 left = vscroll
  71.                 top = hscroll
  72.             
  73.         (width, height) = toplevel.get_size()
  74.         if alloc.y + alloc.height + dy == height and not bottom:
  75.             alloc.height += border.bottom
  76.         if alloc.x + alloc.width + dx == width and not right:
  77.             alloc.width += border.right
  78.         if alloc.y + dy == 0 and not top:
  79.             alloc.y -= border.top
  80.             alloc.height += border.top
  81.         if alloc.x + dx == 0 and not left:
  82.             alloc.x -= border.left
  83.             alloc.width += border.left
  84.         return Gtk.ScrolledWindow.do_size_allocate(self, alloc)
  85.  
  86.  
  87.  
  88. class Notebook(Gtk.Notebook):
  89.     """A regular gtk.Notebook, except when appending a page, if no
  90.     label is given, the page's 'title' attribute (either a string or
  91.     a widget) is used."""
  92.     
  93.     def __init__(self, *args, **kwargs):
  94.         super(Notebook, self).__init__(*args, **kwargs)
  95.         self.connect('key-press-event', self._Notebook__key_pressed)
  96.  
  97.     
  98.     def __key_pressed(self, widget, event):
  99.         for i in xrange(self.get_n_pages()):
  100.             if is_accel(event, '<alt>%d' % (i + 1)):
  101.                 self.set_current_page(i)
  102.                 return True
  103.         
  104.         return False
  105.  
  106.     
  107.     def do_size_allocate(self, alloc):
  108.         ctx = self.get_style_context()
  109.         border = ctx.get_border(self.get_state_flags())
  110.         toplevel = self.get_toplevel()
  111.         top_window = toplevel.get_window()
  112.         window = self.get_window()
  113.         if not window:
  114.             GLib.idle_add(self.queue_resize)
  115.             return Gtk.Notebook.do_size_allocate(self, alloc)
  116.         (dummy, x1, y1) = None.get_origin()
  117.         (dummy, x2, y2) = window.get_origin()
  118.         dx = x2 - x1
  119.         dy = y2 - y1
  120.         border.left = border.top = border.right = border.bottom = 1
  121.         (width, height) = toplevel.get_size()
  122.         if alloc.y + alloc.height + dy == height:
  123.             alloc.height += border.bottom
  124.         if alloc.x + alloc.width + dx == width:
  125.             alloc.width += border.right
  126.         if alloc.x + dx == 0:
  127.             alloc.x -= border.left
  128.             alloc.width += border.left
  129.         return Gtk.Notebook.do_size_allocate(self, alloc)
  130.  
  131.     
  132.     def append_page(self, page, label = None):
  133.         if label is None:
  134.             
  135.             try:
  136.                 label = page.title
  137.             except AttributeError:
  138.                 raise TypeError('no page.title and no label given')
  139.             
  140.  
  141.         if not isinstance(label, Gtk.Widget):
  142.             label = Gtk.Label(label = label)
  143.         super(Notebook, self).append_page(page, label)
  144.  
  145.  
  146.  
  147. def Frame(label, child = None):
  148.     '''A Gtk.Frame with no shadow, 12px left padding, and 3px top padding.'''
  149.     frame = Gtk.Frame()
  150.     label_w = Gtk.Label()
  151.     label_w.set_markup('<b>%s</b>' % util.escape(label))
  152.     align = Gtk.Alignment(xalign = 0, yalign = 0, xscale = 1, yscale = 1)
  153.     align.set_padding(6, 0, 12, 0)
  154.     frame.add(align)
  155.     frame.set_shadow_type(Gtk.ShadowType.NONE)
  156.     frame.set_label_widget(label_w)
  157.     if child:
  158.         align.add(child)
  159.         label_w.set_mnemonic_widget(child)
  160.         label_w.set_use_underline(True)
  161.     return frame
  162.  
  163.  
  164. def Alignment(child = None, top = 0, bottom = 0, left = 0, right = 0, border = 0, **kwargs):
  165.     align = Gtk.Alignment(**kwargs)
  166.     align.set_padding(top + border, bottom + border, left + border, right + border)
  167.     if child:
  168.         align.add(child)
  169.     return align
  170.  
  171.  
  172. def MenuItem(label, stock_id):
  173.     '''An ImageMenuItem with a custom label and stock image.'''
  174.     item = Gtk.ImageMenuItem.new_with_mnemonic(label)
  175.     item.set_always_show_image(True)
  176.     if Gtk.stock_lookup(stock_id):
  177.         image = Gtk.Image.new_from_stock(stock_id, Gtk.IconSize.MENU)
  178.     else:
  179.         image = Gtk.Image.new_from_icon_name(stock_id, Gtk.IconSize.MENU)
  180.     image.show()
  181.     item.set_image(image)
  182.     return item
  183.  
  184.  
  185. def Button(label, stock_id, size = Gtk.IconSize.BUTTON):
  186.     '''A Button with a custom label and stock image. It should pack
  187.     exactly like a stock button.'''
  188.     align = Gtk.Alignment(xscale = 0, yscale = 1, xalign = 0.5, yalign = 0.5)
  189.     hbox = Gtk.HBox(spacing = 2)
  190.     if Gtk.stock_lookup(stock_id):
  191.         image = Gtk.Image.new_from_stock(stock_id, size)
  192.     else:
  193.         image = Gtk.Image.new_from_icon_name(stock_id, size)
  194.     hbox.pack_start(image, True, True, 0)
  195.     label = Gtk.Label(label = label)
  196.     label.set_use_underline(True)
  197.     hbox.pack_start(label, True, True, 0)
  198.     align.add(hbox)
  199.     align.show_all()
  200.     button = Gtk.Button()
  201.     button.add(align)
  202.     return button
  203.  
  204.  
  205. class Paned(Gtk.Paned):
  206.     
  207.     def __init__(self, *args, **kwargs):
  208.         super(Paned, self).__init__(*args, **kwargs)
  209.         self.ensure_wide_handle()
  210.  
  211.     
  212.     def ensure_wide_handle(self):
  213.         if hasattr(self.props, 'wide_handle'):
  214.             self.props.wide_handle = True
  215.             add_css(self, '\n                GtkPaned {\n                    border-width: 0;\n                }\n            ')
  216.             return None
  217.         None(self, '\n            GtkPaned {\n                -GtkPaned-handle-size: 6;\n                background-image: none;\n                margin: 0;\n                border-width: 0;\n            }\n        ')
  218.  
  219.  
  220.  
  221. class RPaned(Paned):
  222.     '''A Paned that supports relative (percentage) width/height setting.'''
  223.     ORIENTATION = None
  224.     
  225.     def __init__(self, *args, **kwargs):
  226.         if self.ORIENTATION is not None:
  227.             kwargs['orientation'] = self.ORIENTATION
  228.         super(RPaned, self).__init__(*args, **kwargs)
  229.         self._RPaned__alloced = False
  230.         self._RPaned__relative = None
  231.  
  232.     
  233.     def set_relative(self, v):
  234.         '''Set the relative position of the separator, [0..1].'''
  235.         if self._RPaned__alloced:
  236.             max_pos = self.get_property('max-position')
  237.             if not max_pos:
  238.                 self._RPaned__relative = v
  239.                 return None
  240.             None.set_position(int(v * max_pos))
  241.         else:
  242.             self._RPaned__relative = v
  243.  
  244.     
  245.     def get_relative(self):
  246.         '''Return the relative position of the separator, [0..1].'''
  247.         if self._RPaned__alloced:
  248.             max_pos = self.get_property('max-position')
  249.             if not max_pos:
  250.                 return self._RPaned__relative
  251.             return None(self.get_position()) / max_pos
  252.         if None._RPaned__relative is not None:
  253.             return self._RPaned__relative
  254.         return None
  255.  
  256.     
  257.     def do_size_allocate(self, *args):
  258.         ret = Gtk.HPaned.do_size_allocate(self, *args)
  259.         if not (self._RPaned__alloced) and self._RPaned__relative is not None:
  260.             self._RPaned__alloced = True
  261.             self.set_relative(self._RPaned__relative)
  262.             ret = Gtk.HPaned.do_size_allocate(self, *args)
  263.         self._RPaned__alloced = True
  264.         return ret
  265.  
  266.  
  267.  
  268. class RHPaned(RPaned):
  269.     ORIENTATION = Gtk.Orientation.HORIZONTAL
  270.  
  271.  
  272. class RVPaned(RPaned):
  273.     ORIENTATION = Gtk.Orientation.VERTICAL
  274.  
  275.  
  276. class ConfigRPaned(RPaned):
  277.     
  278.     def __init__(self, section, option, default, *args, **kwargs):
  279.         super(ConfigRPaned, self).__init__(*args, **kwargs)
  280.         self.set_relative(config.getfloat(section, option, default))
  281.         self.connect('notify::position', self._ConfigRPaned__changed, section, option)
  282.  
  283.     
  284.     def __changed(self, widget, event, section, option):
  285.         if self.get_property('position-set'):
  286.             config.set(section, option, str(self.get_relative()))
  287.  
  288.  
  289.  
  290. class ConfigRHPaned(ConfigRPaned):
  291.     ORIENTATION = Gtk.Orientation.HORIZONTAL
  292.  
  293.  
  294. class ConfigRVPaned(ConfigRPaned):
  295.     ORIENTATION = Gtk.Orientation.VERTICAL
  296.  
  297.  
  298. class _SmallImageButton(object):
  299.     '''A button for images with less padding'''
  300.     
  301.     def __init__(self, **kwargs):
  302.         super(_SmallImageButton, self).__init__(**kwargs)
  303.         self.set_size_request(26, 26)
  304.         add_css(self, '\n            * {\n                padding: 0px;\n            }\n        ')
  305.  
  306.  
  307.  
  308. class SmallImageButton(_SmallImageButton, Gtk.Button):
  309.     pass
  310.  
  311.  
  312. class SmallImageToggleButton(_SmallImageButton, Gtk.ToggleButton):
  313.     pass
  314.  
  315.  
  316. def ClearButton(entry = None):
  317.     clear = Gtk.Button()
  318.     clear.add(Gtk.Image.new_from_stock(Gtk.STOCK_CLEAR, Gtk.IconSize.MENU))
  319.     clear.set_tooltip_text(_('Clear search'))
  320.     if entry is not None:
  321.         connect_obj(clear, 'clicked', entry.set_text, '')
  322.     return clear
  323.  
  324.  
  325. def EntryCompletion(words):
  326.     '''Simple string completion.'''
  327.     model = Gtk.ListStore(str)
  328.     for word in sorted(words):
  329.         model.append(row = [
  330.             word])
  331.     
  332.     comp = Gtk.EntryCompletion()
  333.     comp.set_model(model)
  334.     comp.set_text_column(0)
  335.     return comp
  336.  
  337.  
  338. def RadioMenuItem(*args, **kwargs):
  339.     '''RadioMenuItem that allows None for group'''
  340.     if kwargs.get('group', None) is None:
  341.         kwargs.pop('group', None)
  342.     return Gtk.RadioMenuItem(*args, **kwargs)
  343.  
  344.  
  345. def SeparatorMenuItem(*args, **kwargs):
  346.     if not args and not kwargs:
  347.         return Gtk.SeparatorMenuItem.new()
  348.     return None.SeparatorMenuItem(*args, **kwargs)
  349.  
  350.  
  351. def SymbolicIconImage(name, size, fallbacks = None):
  352.     """Gtk.Image that displays a symbolic version of 'name' and falls
  353.     back to the non-symbolic one.
  354.     """
  355.     symbolic_name = name + '-symbolic'
  356.     gicon = Gio.ThemedIcon.new_from_names([
  357.         symbolic_name,
  358.         name])
  359.     return Gtk.Image.new_from_gicon(gicon, size)
  360.  
  361.